package org.example.book.mediator;


import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class Medicator extends AbstractMedicator{

    /**
     * 外层的key为orderId， 内层的key为Buyer和Payer，value为AbstractCustomer
     */
    public static Map<String, Map<String, AbstractCustomer>> customerInstances = new HashMap<String, Map<String, AbstractCustomer>>();

//    private AbstractCustomer buyer;
//    private AbstractCustomer payer;
//
//    public void setBuyer(AbstractCustomer buyer) {
//        this.buyer = buyer;
//    }
//
//    public void setPayer(AbstractCustomer payer) {
//        this.payer = payer;
//    }

    @Override
    public void messageTransfer(String orderId, String targer, AbstractCustomer customer, String payResult) {
        if (customer instanceof Buyer) {
            AbstractCustomer buyer = customerInstances.get(orderId).get("buyer");
            System.out.println("朋友代付：" + buyer.getCustomerName() + "转发 orderId " + orderId + " 到用户 " + targer + " 进行支付。");
        } else if (customer instanceof Payer) {
            AbstractCustomer payer = customerInstances.get(orderId).get("payer");
            System.out.println("代付完成：" + payer.getCustomerName() + " 完成OrderId " + orderId + " 的支付，通知 " + targer + " 支付结果" + payResult);
            customerInstances.remove(orderId);
        }
    }
}
